package graph.day15;

import java.util.Stack;

public class Leetcode_230 {

    Stack<TreeNode> stack = new Stack<>();

    public int kthSmallest(TreeNode root, int k) {
        if (root == null) return 0;
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                --k;
                if (k == 0) break;
                cur = cur.right;
            }
        }
        return cur.val;
    }

}
